package com.team20.map_demo.controller;

import com.team20.map_demo.domain.Doorplate;
import com.team20.map_demo.domain.Street;
import com.team20.map_demo.service.DoorplateService;
import com.team20.map_demo.service.RegionService;
import com.team20.map_demo.service.StreetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("doorplate")
public class DoorplateController {
    @Autowired
    private DoorplateService doorplateService;
    @Autowired
    RegionService regionService;
    @Autowired
    StreetService streetService;

    @RequestMapping(value = "/search", method = RequestMethod.GET)
    public String doorplatePage(Model model) {
        List<Doorplate> doorplateList = doorplateService.findAllDoorplate();
        model.addAttribute("doorplate", doorplateList);
        return "doorplate_list1";
    }
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String addDoorplate(Model model){
        model.addAttribute("doorpalte",new Doorplate());
        return "doorplate_add";
    }
    @GetMapping("/edit")
        public String editDoorplatePage(@RequestParam("id") String id, Model model){
            Doorplate doorplate=doorplateService.findDoorplateByPrimaryKey(id);
            model.addAttribute("doorplate",doorplate);
            return "doorplate_add";
    }
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public  String saveDoor(Doorplate doorplate){
        int update=doorplateService.saveDoorplate(doorplate);
        return "redirect:/doorplate/search";
    }
    @DeleteMapping("delete")
    @ResponseBody
    public Boolean deleteDoorplate(@RequestParam("id") String id,Model model){
        int deleted =  doorplateService.deleteDoorplateByPrimsryKey(id);
        List<Doorplate> doorplateList = doorplateService.findAllDoorplate();
        model.addAttribute("doorplate", doorplateList);
        return deleted > 0;

    }
    //根据行政区获取街道
    @RequestMapping(value = "/getStreetByDistrictId")
    @ResponseBody
    public List<Street> getStreetBytRId(String id){
        List<Street> streetList=streetService.getStreetBytRId(id);
        if(streetList!=null){
            return streetList;
        }else {
            return null;
        }
    }

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public String regionStreet(Model model){
        model.addAttribute("regionList",regionService.selectAllRegion());
        return "statistic";
    }
    @RequestMapping(value = "/list2",method = RequestMethod.GET)
    public String regionStreet1(Model model){
        model.addAttribute("regionList",regionService.selectAllRegion());
        return "statistics2";
    }

     //根据街道获取门牌
   @RequestMapping(value = "/getDoorplateByStreetId")
    @ResponseBody
    public List<Doorplate> getDoorplateByStreetId(String id){
        List<Doorplate> doorplateList = doorplateService.getDoorplateByStreetId(id);
        if(doorplateList != null){
            return doorplateList;
        }else{
            return null;
        }
    }

    @RequestMapping("streetDoorplateData")
    public String streetDoorplateData(String streetName,Model model){
        List<Doorplate> doorplates = doorplateService.streetDoorplate(streetName);
        model.addAttribute("doorplates",doorplates);
        return "statistic";
    }
 @RequestMapping(value = "/req",method = RequestMethod.GET)
    public String addDoorplate(Model model){
        model.addAttribute("doorplate",new Doorplate());
        return "doorplate-app";

    }
    @RequestMapping(value = "/req",method = RequestMethod.POST)
    public String saveDoorplate(Doorplate doorplate){
        int update=doorplateService.saveDoorplate(doorplate);
        return "redirect:/doorplate/search";
    }
    public List<Doorplate> findDoorplate(){
        List<Doorplate> doorplates =doorplateService.findDoorplateByName();
        return doorplates;

}
